﻿$(document).ready(function () {

    //菜单树
    GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/GetAllMenu", "", function () {
    	    if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                    var txt= parseResponseText(xmlhttp.responseText)
						                     if (txt!= "") {
						                    	 var d=JSON.parse(txt);
                                $("#depttree").combotree('loadData',d );
                            }
						                }
						                }
    
    });
    //图标树
//    GetSmJSON(CLDJServiceUrl + "/SmwsFrame.asmx/GetMenuIcons", "{}", function (d) {
//        if (d != "") {
//            $("#icontree").combotree('loadData', JSON.parse(d));
//        }
//    });

    var myDept = new SmObjectData("BASE_DEPT");

    $('#deptgrid').treegrid({
        title:"部门管理",
        idField: 'DEPT_ID',
        treeField: 'DEPT_NAME',
        columns: [[
            { field: 'DEPT_NAME', title: '部门/员工', width: 200 },
            { field: 'DEPT_DES', title: '部门描述', width: 300 }
//            { field: 'ICON', title: '图标', width: 80 },
//            { field: 'BZ', title: '模块说明', width: 80 },
//            { field: 'ORDER_NUM', title: '显示序号', width: 100 }
        ]],
        toolbar:
            [{
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    myDept.Init("DEPT_ID", "");
                    GetValues();
                    $("#deptdlg").dialog("open");
                }
            }, '-', {
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    var node = $('#deptgrid').treegrid('getSelected');
                    if (node == null) {
                        $.messager.alert('提示', '请选择一行进行修改。');
                    } else {
                        myDept.Init("DEPT_ID", node.DEPT_ID);
                        GetValues();
                        $("#deptdlg").dialog("open");
                    }
                    
                }
            }, '-', {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    var node = $('#deptgrid').treegrid('getSelected');
                    if (node == null) {
                        $.messager.alert('提示', '请选择一行进行修改。');
                    } else {
                        $.messager.confirm('警告', '确认要删除选中的菜单及其子菜单吗?', function (r) {
                            if (r) {
                                GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/DeleteDEPT", "DEPT_ID="+node.DEPT_ID, function () {
                                	 if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                   
                                  reloadgrid();
                            
						                }
						                }
                                  
                                });
                            }
                        });

                    }
                }
            }, '-', {
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    reloadgrid();
                }
            }]
    });

    reloadgrid();

    function reloadgrid() {    
        GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/GetDeptUserTreeGrid", "", function () {
        		 if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                                  var txt= parseResponseText(xmlhttp.responseText)
						                     if (txt!= "") {
                                $('#deptgrid').treegrid('loadData', JSON.parse(txt));
                            }                            
						                }
						                }
        });
    }

    function GetValues() {
        $("#DEPT_ID").val(myDept.GetValue("DEPT_ID"));
        $("#DEPT_NAME").val(myDept.GetValue("DEPT_NAME"));
        $("#DEPT_DES").val(myDept.GetValue("DEPT_DES"));

    }
    function SetValues() {
        myDept.SetValue("DEPT_NAME", $("#deptName").val());
        myDept.SetValue("DEPT_DES", $("#DEPT_DES").val());
    }

    $("#btnSave").click(function () {
        SetValues();
        myDept.Update();
        reloadgrid();
        $("#deptdlg").dialog("close");
    });

    $("#btnCance").click(function () {
        $("#deptdlg").dialog("close");
    });


});